﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using FDStudio.Core.Entities;
using FDStudio.Core.Entities.Query;
using FDStudio.Core.Repositories;

namespace FDStudio.Core.Services
{
    /*
     CreatedOn: 2018-03-08
     CreatedBy: Jackie Lee(天宇遊龍)
     */
    /// <summary>
    /// 只读服务基类
    /// </summary>
    public class ReadOnlyServiceBase<T> : IReadOnlyService<T> where T : class, IEntity
    {
        private IPagedReadRepository<T> _repository;

        public ReadOnlyServiceBase(IPagedReadRepository<T> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取当仓储
        /// </summary>
        /// <typeparam name="U"></typeparam>
        /// <returns></returns>
        protected U Repository<U>()
        {
            return (U)_repository;
        }

        public virtual T Get(long id)
        {
            return _repository.Get(id);
        }

        public virtual T Get(Expression<Func<T, bool>> condition)
        {
            return _repository.Get(condition);
        }

        public virtual IEnumerable<T> GetList(Expression<Func<T, bool>> condition, IQueryOrder<T> orderBy = null)
        {
            return _repository.GetList(condition, orderBy);
        }

        public virtual PagedList<T> GetList(Expression<Func<T, bool>> condition, long pageIndex, long pageSize, IQueryOrder<T> orderBy = null)
        {
            return _repository.GetList(condition, pageIndex, pageSize, orderBy);
        }

        public bool Exists(Expression<Func<T, bool>> condition)
        {
            return _repository.Exists(condition);
        }
    }
}
